package site.yunnong.atvris.portal.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;
import site.yunnong.atvris.common.utils.FrameGrabberUtils;
import site.yunnong.atvris.dao.domain.Video;
import site.yunnong.atvris.dao.domain.VideoExample;
import site.yunnong.atvris.dao.domain.VideoProfile;
import site.yunnong.atvris.dao.domain.VideoProfileExample;
import site.yunnong.atvris.dao.mapper.VideoMapper;
import site.yunnong.atvris.dao.mapper.VideoProfileMapper;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zjh
 * @date 2021/8/31 17:27
 */
@Component
@Slf4j
public class FixDataUtil {
    
    @Resource
    private VideoMapper videoMapper;
    @Resource
    private VideoProfileMapper videoProfileMapper;
    
    public void fixVideoTime() {
        log.info("start fix video time...");
        VideoExample videoExample = new VideoExample();
        videoExample.createCriteria().andVideoTimeEqualTo(0L);
        List<Video> videos = videoMapper.selectByExample(videoExample);
        for (Video video : videos) {
            String url = video.getUrl();
            try {
                long videoTime = FrameGrabberUtils.getVideoTime(url);
                video.setVideoTime(videoTime);
            } catch (Exception e) {
                continue;
            }
            log.info("fix video time, id: {}", video.getId());
            videoMapper.updateByPrimaryKey(video);
            log.info("fix video time, id: {}, SUCCESS", video.getId());
        }
        log.info("fix video time finished...");
    }

    public void fixVideoPartition() {
    }

    public void fixVideoProfile() {
        log.info("start fix video profile...");
        List<Video> videos = videoMapper.selectByExample(new VideoExample());
        for (Video video : videos) {
            VideoProfileExample videoProfileExample = new VideoProfileExample();
            videoProfileExample.createCriteria().andUvidEqualTo(video.getId());
            List<VideoProfile> videoProfiles = videoProfileMapper.selectByExample(videoProfileExample);
            log.info("fix video profile, vid: {}", video.getId());
            if (CollectionUtils.isEmpty(videoProfiles)) {
                // 没有videoProfile
                VideoProfile videoProfile = VideoProfile.builder().uvid(video.getUvid())
                        .build();
                videoProfileMapper.insertSelective(videoProfile);

            } else {
                // 有videoProfile
                if (!video.getUvid().equals(videoProfiles.get(0).getUvid())) {
                    // 如果uvid不符合，修复
                    videoProfiles.get(0).setUvid(video.getUvid());
                    videoProfileMapper.updateByPrimaryKeySelective(videoProfiles.get(0));
                }
            }
            log.info("fix video profile, vid: {}", video.getId());
        }
        log.info("fix video profile finished...");
    }

    public void markVideoTags() {
    }

    public void fixVideoUvid() {
    }
}
